<?php

/**
 * 教练页面
 * 作者：*****
 *
 */
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\model\Service;
use App\model\Oil;
use App\model\Change_car;
use App\Http\Requests;
use App\model\Student;
use App\model\User;
use App\model\Comment;
use App\model\Attitude;
use App\model\Use_stu;
use App\model\Papers;
use App\model\Subject;
use App\model\Order;

class CoachController extends Controller
{ 

   /**
    * 个人信息
    *
    */

    public function user(Request $request)
    {
        $model = new User();
        $u_id = 1;

        if($request->all())
        {
            $arr = $request->all();
            $edit = $model->edituser($arr);
            if($edit)
            {
                return redirect('coach.user');
            }
        }else
        {
            $user = $model->getData($u_id);
            return view('coach.user',compact('user'));
        }
    }

   /**
    * 评价考核
    *
    */

    public function assess()
    {
        $model = new Comment();
        $mode = $model->comment()->toArray();
        $average =$model->average();
        $count = count($model->comment()->toArray());
        $counts = $model->one()->toArray();
        foreach ($counts as $key => $val) 
        {
          $raise = $val['com_appraise'];
          $img = $val['use_pic'];
        }

        foreach ($mode as $k => $v) 
        {
                $serve = $v['com_service'];
                $teach = $v['com_teach'];
                $behavior = $v['com_behavior'];
                $honest = $v['com_clean'];
        }

        $mean = ($serve+$teach+$behavior+$honest)*$count/$count;
        
        $maxs = new Attitude();

        $max=$maxs->check();
        $min = $maxs->least();
        $satisficing = $maxs->satisficing();
        $satisficingleast = $maxs->satisficingleast();
        $qualifiedcing = $maxs->qualifiedcing();
        $qualifiedleast = $maxs->qualifiedleast();
        
        $averages = ($max+$min)/2;
        $satisfy = ($satisficing+$satisficingleast)/2;
        $certification = ($qualifiedcing+$qualifiedleast)/2;

        $reservation =($max+$min+$averages)/3;
        $satisfied = ($satisficing+$satisficingleast+$satisfy)/3;
        $eligible = ($qualifiedcing+$qualifiedleast+$certification)/3;
        
        return view('coach.assess',[
          'serve' =>$serve,
          'teach' =>$teach,
          'behavior' =>$behavior,
          'honest' =>$honest,
          'count' =>$count,
          'raise' =>$raise,
          'average' =>$average,
          'img' =>$img,
          'mean' =>$mean,
          'max' =>$max,
          'min' =>$min,
          'averages' =>$averages,
          'reservation' =>$reservation,
          'satisficing' =>$satisficing,
          'satisficingleast' =>$satisficingleast,
          'satisfy' =>$satisfy,
          'satisfied' =>$satisfied,
          'qualifiedcing' =>$qualifiedcing,
          'qualifiedleast' =>$qualifiedleast,
          'certification' =>$certification,
          'eligible' =>$eligible,
          ]);
    }

   /**
    * 我的学员
    *
    */

    public function student()
    {
        $arr = new Student(); 
        $id=2;
        $model = $arr->getstudent($id);
        $re = new Use_stu();
        $query = $re->selectState();
        $pap = new Papers();
        $pap_info =$pap->paperslists();
        $sub = new Subject();
        $sub_info = $sub->selectSubject();
        return view('coach.student',[
          'model' => $model,
          'query' =>$query,
          'pap_info' =>$pap_info,
          'sub_info' => $sub_info,
          ]);
    }
    
   /**
    * 学员进度详情
    *
    */
   public function details()
   {   
       $arr = new User();
       $model = $arr->basic()->toArray();
       $info = $arr->phase();
       $ones = new Student();
       $one = $ones->one();
       $two = $ones->two();
       $three = $ones->three();
       $four = $ones->four();
       $five = $ones->five();
       $whens = $ones->when()->toArray();
       $when = "images/".$whens['sub_id'].".jpg"; 
       return view('coach.details',[
        'model' => $model,
        'one' => $one,
        'two' => $two,
        'three' => $three,
        'four' => $four,
        'five' => $five,
        'when' =>$when,
        ]);
   }

   /**
    * 换车记录
    *
    */

   public function transfer()
   {   
       $arr = new Change_car();
       $model=$arr->transfer();
   	   return view('coach.transfer',[
        'model' => $model,
        ]);
   }

   /**
    * 维修记录
    *
    */

   public function maintain()
   {   
       $arr = new Service();
       $model = $arr->repair();
   	   return view('coach.maintain',[
        'model' => $model,
        ]);
   }

   /**
    * 我的排课
    *
    */

   public function scheduling()
   {   
       $use_id=session('use_id');
       $arr = new Order();
       $user=new User();
       $data=array();
       $model = $arr->scheduling($use_id);
       $model=json_encode ( $model );
       $model=json_decode($model,true);
       //var_dump($model);die;
       foreach ($model as $key => $value) 
       {
         $res=explode(',',$value['ord_stu_id']);
         $str=array();
         foreach ($res as $k => $v) 
         {
           if($v==0){
            $name=array('use_name'=>0);
           }else{
           $name=User::select('use_name')->where('use_id',$v)->first();
           $name=json_encode ( $name );
           $name=json_decode($name,true);
           }
           $str[]=$name['use_name']; 
         }
       
        $model[$key]['ord_stu_id']=$str;
       }
       //print_r($model);die;
      return view('coach.scheduling',[
        'model' =>$model,
        ]);
   }
   /**
    * 油气
    *
    */

   public function refuel()
   {   
       $date = date('Y');
       $arr = new Oil();
       $oil = $arr->oil($date);
       $info = $arr->oillist();
       return view('coach.refuel',[
        'arr' =>$oil,
        'info' =>$info,
        ]);
   }

    /*
     * 学员搜索
     *   
     */

    public function xue(Request $request)
    {
        $search=$request->all();
        $student= new Student();
        $id=2; //用户也就是教练的ID
        $model=$student->selectIdInfo($search,$id);
        $re = new Use_stu();
        $query = $re->selectState();
        $pap = new Papers();
        $pap_info =$pap->paperslists();
        $sub = new Subject();
        $sub_info = $sub->selectSubject();

        header('Content-type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename=在校学员.xls');
        $str="序号\t姓名\t编号\t身份证号\t性别\t手机号\t车型\t学习阶段\t报名时间\t状态\n";
        foreach($model as $k=>$v){
            $str.="$k+1\t$v->use_name\t$v->use_num\t$v->use_card\t$v->use_sex\t$v->use_phone\t$v->pap_name\t$v->sub_name\t$v->use_enty\t$v->ust_state\n";
        }

        return view('coach.mentee',[
          'model' =>$model,
          'query' =>$query,
          'pap_info' =>$pap_info,
          'sub_info' =>$sub_info
          ]);
    }

}

